<template>
  <div class="home_container">
    <!-- 顶部搜索导航 -->
    <van-nav-bar
      class="page-nav-bar"
      fixed>
      <van-button icon="search"
      slot="title"
      round
      type="info"
      class="search_btn"
      to="/search">搜索</van-button>
    </van-nav-bar>

    <!-- tab 切换页面 -->
    <van-tabs class="home-tab" v-model="active"
      animatedhome-tab
      swipeable>
        <van-tab :key="item.id" v-for="item in Channels" :title="item.name">
          <article-list :channel="item"></article-list>
        </van-tab>
        <!-- 这个盒子不能设置宽度，已经不能参与flex计算，除非些内容或者不参与flex计算 -->
        <!-- 或者设置padding -->
         <div slot="nav-right" class="placeholder_box"></div>
         <div @click="isShowEditPopup = true" slot="nav-right" class="hamburger_btn">
           <i class="iconfont icon-gengduo"></i>
         </div>
      </van-tabs>

      <!-- 频道编辑弹出层 -->
      <van-popup
        v-model="isShowEditPopup"
        closeable
        close-icon-position="top-left"
        position="bottom"
        :style="{ height: '90%' }">
        <channel-edit
        @updateActive="editActive"
        :myChannels="Channels"
        :active='active'>
        </channel-edit>
      </van-popup>

  </div>
</template>

<script>
import { getChannels } from '@/api/user'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'home',
  data () {
    return {
      active: '',
      Channels: [],
      isShowEditPopup: false
    }
  },

  created () {
    // getChannels()
    this.loadChannels()
  },

  methods: {
    async loadChannels () {
    // 加载首页用户的频道列表
    // 如果已登录，发送请求获取回来
    // 如果未登录且本地没有数据，也需要发送请求
    // 如果用户未登录，且本地有数据，就获取本地数据
      const localChannels = getItem('http_channels')
      if (!this.user) {
        this.Channels = localChannels
        return false
      }
      // 那就判断一下未登录求本地已有数据的条件，否则都需要发送请求
      try {
        const { data: res } = await getChannels()
        // console.log(res)
        this.Channels = res.data.channels
      } catch (err) {
        this.$toast.fail('获取频道失败')
      }
    },
    // 更新Active
    editActive (index, flag = false) {
      this.active = index
      this.isShowEditPopup = flag
    }
  },
  components: {
    // 注册组件，驼峰式命名
    ArticleList,
    ChannelEdit,
    ...mapState(['user'])
  }
}
</script>

<style scoped lang='less'>
 // 容器样式
 .home_container {
    padding-bottom: 100px;
    /deep/.van-nav-bar__title {
     max-width: unset;
  }
  .search_btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, .2);
    border-radius: 32px;
  }
  .van-nav-bar .van-icon {
    color: #fff;
  }
  // tabs样式
  /deep/.van-tabs {
     padding-top: 172px;
     .van-tabs__wrap {
      position: fixed;
      top: 92px;
      width: 100%;
      z-index: 2;
    }
    .van-tabs__nav {
      padding-bottom: 0;
      height: 80px;
    }
    .van-tab {
      min-width: 190px;
      border: 1px solid #eee;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3295F9;
      border-radius: 3px;
      bottom: 8px;
    }
    // 汉堡菜单
    .placeholder_box {
      width: 66px;
      height: 88px;
      flex-shrink: 0;
    }
    .hamburger_btn {
      width: 66px;
      height: 88px;
      position: fixed;
      right: 0;
      background: #fff;
      text-align: center;
      line-height: 82px;
      opacity: 0.9;
      .icon-gengduo {
        font-size: 40px;
      }
      &:before {
        content: '';
        width: 2px;
        height: 100%;
        background: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
        position: absolute;
        left: 0;
      }
    }
  }
}
</style>
